import { Component, OnInit, ViewChild } from '@angular/core';

import { Service } from '../../../server/server';
import { InitService } from '../../../public/serve/init/init.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';

import { appProductUrl } from '../../product.url';
import { product } from '../../product.data';
import { taskAddEimt } from "../product-task-list";

import { ProductIndexingTaskListTableComponent } from './table/product-indexing-task-list-table.component';
import { ActivatedRoute, Params, Router, NavigationEnd } from '@angular/router';
import {appThemeTagUrl} from '../../../system/theme-tag/theme-tag.url';
declare var $:any;
@Component({
    selector: 'app-product-indexing-task-list',
    templateUrl: './product-indexing-task-list.component.html',
    styleUrls: ['./product-indexing-task-list.component.css']
})
export class ProductIndexingTaskListComponent implements OnInit {
    @ViewChild("table") table : ProductIndexingTaskListTableComponent; 
    isActive = this.server.isActive;
    taskId: string = '';

    taskType: any = '0';
    right: any = {
        "taskPermissionList":{},
        "productList": {},
        "internalList": {},
        "themeTaskList":{}
    };

    constructor(
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public request: CudTableService,
    public server: Service,
    private hintsService: HintsService,
    private activatedRoute: ActivatedRoute,
    private router: Router,
    ) { 
        this.right = $.extend(this.right, server.getRight());
    }

    ngOnInit() {
        this.activatedRoute.queryParams.subscribe((p: Params) => {
            this.taskType = String(p['taskType']);

            //权限
            if(this.taskType == '1'){
                //内文任务
                this.right['taskPermissionList'] = this.right['internalList'];
            }else if(this.taskType == '0'){
                //产品任务
                this.right['taskPermissionList'] = this.right['productList'];
            }else if(this.taskType == '2'){
                //主题词任务
                this.right['taskPermissionList'] = this.right['themeTaskList'];
            }
        });

        this.server.eventEmit.subscribe((value: any) => {
            this.isActive = value;
        });

    }

    /**
      * 分页
      * @param pagination 
      */
    _pagination(pagination: Array<any>) {
        product.pagination.pageNumber = pagination[0].pageNumber;
        product.pagination.pageSize = pagination[0].pageSize;
        product.pagination.naturalNum = (product.pagination.pageNumber - 1) * product.pagination.pageSize;
        this.table.getTasksList();
    }

    /**
     * 搜索
     * @param val
     */
    _retrieval(val: string) {
        product.retrieval.queryCriteria = val.trim();
        this.table.getTasksList();
    }

    /**
     * 新增主任务
     */
    addNewRootTask() {
        taskAddEimt.emit({ 'data': '', 'type': 'mainTask' });
    }

    /**
     * 导出标引结果
     */
    exportClick(){
        let data:any = {
            taskId          : this.removeTableService.getRemoveTableId,
            productId      : '',
            themeTableId    : '',
            themeTableName  : '',
            pathCode        : '',
            exportPath      : '',
            exportType      : '',
            createBy        : '',
            createTime      : '',
            description     : '',

        }
        this.request.postRequest(this.server.baseUrl +appProductUrl.exportIndexingThemeWords,data).then((res: any) => {
            this.hintsService.messages(res.message, res.result_code);
            if (res.result_code === '0000') {
                console.log(res);
            }
        }).catch((err: any) => {
            this.hintsService.messages("error", "error");
            return;
        });
    }
}
